package generics;

import generics.coffee.Americano;
import generics.coffee.Breve;
import generics.coffee.Coffee;
import generics.coffee.Latte;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 杨宇
 * @version 1.0 2016/11/18
 */
public class E35_CheckedList {

    @SuppressWarnings("unchecked")
    public static void oldStyleMethod(List list) {
        list.add(new Breve());
    }

    public static void main(String[] args) {
        List<Latte> lattes1 = new ArrayList<>();
        oldStyleMethod(lattes1);

        List<Latte> lattes2 = Collections.checkedList(new ArrayList<Latte>(), Latte.class);
        try {
            oldStyleMethod(lattes2);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        List<Coffee> coffees = Collections.checkedList(new ArrayList<Coffee>(), Coffee.class);
        coffees.add(new Latte());
        coffees.add(new Breve());

    }

}
